/**
 * 
 */
package scriptrunner.execution;

import java.util.Map;
import java.util.TreeMap;

import scriptrunner.execution.cloud.CloudFunctionProxy;

/**
 * @author Administrator
 * 
 */
public class FunctionRegister {

	public Function GetFunction(FuncSignature funSig) {
		return funcMap.get(funSig);
	}

	public void RegisterFunction(Function f, boolean remote) {
		if (remote) {
			f = new CloudFunctionProxy(f);
		}
		
		FuncSignature signature = f.GetSignature();

		funcMap.put(signature, f);
	}
	
	public void RegisterFunction(Function f) {
		RegisterFunction(f, true);
	}
	
	public void RegisterPredefinedFunction( String[] funcName ) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		for ( String s : funcName ) {
			Class<?> c = Class.forName(s);
			if ( Function.class.isAssignableFrom(c) ) {
				Function f = (Function)c.newInstance();
				RegisterFunction(f, false);
			}
		}
		
	}

	public void SetMainFunction(MainFunction func) {
		mainFunc = func;
	}

	public MainFunction GetMainFunction() {
		return mainFunc;
	}

	protected Map<FuncSignature, Function> funcMap = new TreeMap<FuncSignature, Function>();
	protected MainFunction mainFunc;

	public static FunctionRegister GetInstance() {
		return funcReg;
	}

	static private FunctionRegister funcReg = new FunctionRegister();

}
